require(['./config'],() => {
    require(['template','header','footer'],(template,header) => {//接收header，改变购物车数量
        class cart{
            constructor(){
                // console.log(localStorage.getItem(cart))
                this.renderCart().then(() => {
                    this.desClick(),
                    this.calTotalMoney(),
                    this.btnClick(),
                    this.delClick()
                    
                })              
            }
            /*******渲染函数*******/
            renderCart(){
                return new Promise(resolve => {
                    // console.log(localStorage.getItem('cart'))
                    //把localstorage取出来渲染
                     const cart = localStorage.getItem('cart');
                    if(cart){
                        //解析，渲染
                        this.cart = JSON.parse(cart);
                        // console.log(this.cart);
                        $('.cart-options-left').html(template('cart-options-left-Template',{cart:this.cart}));
                        resolve();
                    }else{
    
                    }
                   
                })               
            }
           /*******详情的点击事件*******/
            desClick(){
                $('.cart-option-clik a:nth-child(1)' ).on('click',function(){
                    let borther = $(this).siblings();
                    $(this).css('display','none');
                    borther.css('display','block');
                    $('.detailed-information-jiade').css('display','none');
                    $('.detailed-information').css('display','block');
                    
                })
                $('.cart-option-clik a:nth-child(2)').on('click',function(){
                    let borther = $(this).siblings();
                    $(this).css('display','none');
                    borther.css('display','block');
                    $('.detailed-information-jiade').css('display','block');
                    $('.detailed-information').css('display','none');
                })
            }
            /*******删除的点击事件*******/
            delClick(){
                /*获取this*/
                var _this1 = this;
                /*获取btn*/
                $('.cart-option-des span').on('click',function(){
                    /*获取当前要删除的商品的id*/
                    const id1 = $(this).parents('.cart-option').attr('data-id');                    
                    /*遍历，forEach遍历cart里的东西*/
                    _this1.cart.forEach((shop,index) => {
                        if(id1 === shop.id){
                            /*裁剪要删除的那条信息*/
                            _this1.cart.splice(index, 1);
                            localStorage.setItem('cart',JSON.stringify(_this1.cart));
                            /*删除并且页面移除*/
                            $(this).parents('.cart-option').remove();
                        }
                        
                    })
                    //函数调用                  
                    // _this1.btnClick();
                    _this1.calTotalMoney();
                    _this1.renderCart();
                    _this1.delClick();
                    _this1.calTotalMoney();
                    _this1.btnClick();
                    header.cartShowNum();
                })
            }
           /*******计算总额*******/
            calTotalMoney(){
                var expenses;
                var qian;
                /*获取计算总额元素*/
                $('.total:nth-child(1) span:nth-child(3)').html(this.cart.reduce((money,shop) => {
                    money += shop.price*shop.num;
                    qian = money;
                    return money;
                },0).toFixed(2))
                /*对运费进行计算 */
                if(qian >= 800){
                    expenses = 0;   
                    $('.money').html(0.00);          
                }else if(qian <800 && qian >0){
                    expenses = 25;
                    $('.money').html(parseFloat((800-qian).toFixed(2)));
                    // $('.total:nth-child(2) span:nth-child(3)').html('25')
                }
                else{
                    expenses = 0;
                    $('.money').html(0.00);
                }
                var totalMoney = expenses+qian;
                $('.total:nth-child(2) span:nth-child(3)').html(expenses.toFixed(2));
                $('.total:nth-child(3) span:nth-child(3)').html(parseFloat(totalMoney.toFixed(2)));
                
            }
           
            /*******增减事件*******/
            btnClick(){
                var _this = this;
                /*增加按钮*/
                $('.addBtn').on('click',function(){               
                    const id = $(this).parents('.cart-option').attr('data-id');
                    _this.cart = _this.cart.map(shop => {                     
                        if(id == shop.id) shop.num++;
                        return shop;
                    })
                    //函数调用
                    localStorage.setItem('cart',JSON.stringify(_this.cart));
                    _this.renderCart();
                    _this.btnClick();
                    _this.calTotalMoney();
                    _this.delClick();
                    _this.calTotalMoney();
                    header.cartShowNum();

                })
                 /*减少按钮*/
                $('.subBtn').on('click',function() {
                    const id = $(this).parents('.cart-option').attr('data-id');
                    _this.cart = _this.cart.map(shop => {
                        if(id == shop.id){                    
                            if(shop.num > 1) {
                                shop.num--;
                            }else{
                                shop.num = 1;
                            }                         
                        } 
                        return shop;
                    })
                    //函数调用
                    localStorage.setItem('cart',JSON.stringify(_this.cart));
                    _this.renderCart();
                    _this.btnClick();
                    _this.calTotalMoney();
                    _this.delClick();
                    _this.calTotalMoney();
                    header.cartShowNum();
                })
            }
        }
        new cart();
    })
})